package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

import liumengbaio.lolzhangyoubao.Activity.InFo.ZhangYouBao;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/8/24.
 */
public class ListViewAdapter extends BaseAdapter {
    private ZhangYouBao zhangyoubao;
    private Context mContext;

    public ListViewAdapter(ZhangYouBao zhangyoubao, Context mContext) {
        this.zhangyoubao = zhangyoubao;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return zhangyoubao.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return zhangyoubao.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (holder == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lol, null);
            holder.iv_hot = (ImageView) convertView.findViewById(R.id.iv_hot);
            holder.iv_lol = (ImageView) convertView.findViewById(R.id.iv_lol);
            holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
            holder.tv_Date = (TextView) convertView.findViewById(R.id.tv_date);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ZhangYouBao.DataBean bean = zhangyoubao.getData().get(position);
        Picasso.with(mContext).load(bean.getPic_url()).into(holder.iv_lol);
        holder.tv_comment.setText(bean.getComment() + "评");
        holder.tv_title.setText(bean.getTitle());
        int date_published = bean.getPublished();
        long date = date_published * 1000L;
        Date d = new Date(date);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        String dateStr = format.format(d);
        holder.tv_Date.setText(dateStr);
        if (bean.getRecommend() == 1) {
            holder.tv_comment.setVisibility(View.INVISIBLE);
            holder.iv_hot.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    public class ViewHolder {
        private ImageView iv_hot;
        public ImageView iv_lol;
        private TextView tv_title, tv_Date, tv_comment;
    }
}
